#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import time
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes

class EncryptionManager:
    def __init__(self, secret_key: str = "qwertyuiop1234567654zaqwsxcderfv"):
        self.secret_key = secret_key.encode('utf-8')[:32].ljust(32, b'\0')
    
    def aes_encrypt(self, data: str) -> str:
        """AES256加密"""
        iv = get_random_bytes(16)
        cipher = AES.new(self.secret_key, AES.MODE_CBC, iv)
        padded_data = pad(data.encode('utf-8'), AES.block_size)
        encrypted = cipher.encrypt(padded_data)
        return base64.b64encode(iv + encrypted).decode('utf-8')

def main():
    """生成加密的活动发布数据"""
    
    print("🔐 生成加密的测试活动数据...")
    
    # 艺术作品图片
    art_images = [
        "https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_154f5afcc73ab7814a745378c6dd2846_edit_391954352758421.png",
        "https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_06409c288f6c65c4f600bddf9283602f_edit_391871265051142.png",
        "https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_c2cfd9ded965c38680f00a21a01386f4_edit_391916608718322.png"
    ]
    
    # 测试活动数据
    activity_data = {
        "activityTitle": "🍽️ 北京朝阳区聚餐活动",
        "publisherId": 62,
        "publisherNickname": "Flutter测试用户",
        "activityPublishTime": "2025-01-30 18:00:00",
        "longitude": 116.404,
        "latitude": 39.915,
        "activityDetails": "周末聚餐，一起品尝北京美食！地点在朝阳区，欢迎大家参加。",
        "activityLocation": "北京市朝阳区CBD商务区",
        "activityImages": art_images,
        "activityType": ["AA"],
        "activityTags": ["聚餐", "周末", "美食"],
        "activityMaxParticipants": 8,
        "publisherAvatarUrl": "http://app.playtruly.cn/system/avatar.jpg",
        "publisherSignature": "喜欢美食，喜欢交朋友",
        "publisherTags": ["美食爱好者", "测试用户"],
        "free": 1,
        "timestamp": int(time.time())
    }
    
    print("📋 活动数据:")
    print(json.dumps(activity_data, indent=2, ensure_ascii=False))
    print()
    
    # 加密
    encryption_manager = EncryptionManager()
    json_string = json.dumps(activity_data, ensure_ascii=False)
    encrypted_data = encryption_manager.aes_encrypt(json_string)
    
    # 生成最终payload
    final_payload = {"encrypted_data": encrypted_data}
    
    print("🔒 加密后的payload:")
    payload_json = json.dumps(final_payload, ensure_ascii=False)
    print(payload_json)
    print()
    
    # 生成curl命令
    print("🚀 curl命令:")
    print('curl -X POST "http://124.71.77.44:2233/activities/" \\')
    print('  -H "Content-Type: application/json; charset=utf-8" \\')
    print('  -H "Content-Encrypted: true" \\')
    print('  -H "Encryption-Algorithm: AES256" \\')
    print(f'  -d \'{payload_json}\'')
    print()
    
    # 第二个活动
    activity_data2 = {
        "activityTitle": "🏃‍♂️ 北京朝阳公园晨跑",
        "publisherId": 62,
        "publisherNickname": "Flutter测试用户",
        "activityPublishTime": "2025-01-31 07:00:00",
        "longitude": 116.404,
        "latitude": 39.915,
        "activityDetails": "早晨一起在朝阳公园晨跑，锻炼身体，享受新鲜空气！",
        "activityLocation": "北京市朝阳区朝阳公园",
        "activityImages": [
            "https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_cdec2b9dcd00187206c05ebebd34b362_edit_391829318571981.png",
            "https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_d5e6dc5448275fdaee80ce22ae7bab3c_edit_391989959571436.png"
        ],
        "activityType": ["我买单"],
        "activityTags": ["运动", "晨跑", "健康"],
        "activityMaxParticipants": 15,
        "publisherAvatarUrl": "http://app.playtruly.cn/system/avatar.jpg", 
        "publisherSignature": "热爱运动，享受生活",
        "publisherTags": ["运动达人", "测试用户"],
        "free": 1,
        "timestamp": int(time.time()) + 5
    }
    
    # 加密第二个活动
    json_string2 = json.dumps(activity_data2, ensure_ascii=False)
    encrypted_data2 = encryption_manager.aes_encrypt(json_string2)
    final_payload2 = {"encrypted_data": encrypted_data2}
    payload_json2 = json.dumps(final_payload2, ensure_ascii=False)
    
    print("🏃‍♂️ 第二个活动curl命令:")
    print('curl -X POST "http://124.71.77.44:2233/activities/" \\')
    print('  -H "Content-Type: application/json; charset=utf-8" \\')
    print('  -H "Content-Encrypted: true" \\')
    print('  -H "Encryption-Algorithm: AES256" \\')
    print(f'  -d \'{payload_json2}\'')

if __name__ == "__main__":
    main()
